home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
BLAKJACK.ARJ
/
072392B.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-08
|
12KB
|
220 lines
/* Project # 4 b) EXTRA CREDIT A game of Blackjack w/o shuffling */
/* David Candel For Dr. Beheshti in Unix And C, August 1, 1992 */
#include<ctype.h>
#include<stdio.h> /* preprocessor directives */
#include<stdlib.h>
struct deck /* declare a structure to hold the deck of cards */
{
int card_number, value, taken;
char suit_rank[2]; /* an array to hold the card suit and */
}; /* and the card rank */
main () /* main */
{
int a, temp, first, second, i, j, count, value3, value4, sum1, sum2, flag = 0, ace;
char choice1, choice2; /* definition of variables */
float seed;
struct deck deck[51] = /* structure to hold the card number, card */
{ { 1, 1, 0, {'H', 'A'} }, /* value, card suit, and card rank */
{ 2, 10, 0, {'H', 'K'} }, /* definition of the structure */
{ 3, 10, 0, {'H', 'Q'} }, /* allocates space in memory */
{ 4, 10, 0, {'H', 'J'} }, /* contains an array */
{ 5, 10, 0, {'H', 'T'} }, /* of size 52 */
{ 6, 9, 0, {'H', 'N'} },
{ 7, 8, 0, {'H', 'E'} },
{ 8, 7, 0, {'H', 'S'} },
{ 9, 6, 0, {'H', 'X'} },
{ 10, 5, 0, {'H', 'F'} },
{ 11, 4, 0, {'H', 'R'} },
{ 12, 3, 0, {'H', 'H'} },
{ 13, 2, 0, {'H', 'W'} },
{ 14, 1, 0, {'D', 'A'} },
{ 15, 10, 0, {'D', 'K'} },
{ 16, 10, 0, {'D', 'Q'} },
{ 17, 10, 0, {'D', 'J'} },
{ 18, 10, 0, {'D', 'T'} },
{ 19, 9, 0, {'D', 'N'} },
{ 20, 8, 0, {'D', 'E'} },
{ 21, 7, 0, {'D', 'S'} },
{ 22, 6, 0, {'D', 'X'} },
{ 23, 5, 0, {'D', 'F'} },
{ 24, 4, 0, {'D', 'R'} },
{ 25, 3, 0, {'D', 'H'} },
{ 26, 2, 0, {'D', 'W'} },
{ 27, 1, 0, {'C', 'A'} },
{ 29, 10, 0, {'C', 'Q'} },
{ 30, 10, 0, {'C', 'J'} },
{ 31, 10, 0, {'C', 'T'} },
{ 32, 9, 0, {'C', 'N'} },
{ 33, 8, 0, {'C', 'E'} },
{ 34, 7, 0, {'C', 'S'} },
{ 35, 6, 0, {'C', 'X'} },
{ 36, 5, 0, {'C', 'F'} },
{ 37, 4, 0, {'C', 'R'} },
{ 38, 3, 0, {'C', 'H'} },
{ 39, 2, 0, {'C', 'W'} },
{ 40, 1, 0, {'S', 'A'} },
{ 41, 10, 0, {'S', 'K'} },
{ 42, 10, 0, {'S', 'Q'} },
{ 43, 10, 0, {'S', 'J'} },
{ 44, 10, 0, {'S', 'T'} },
{ 45, 9, 0, {'S', 'N'} },
{ 46, 8, 0, {'S', 'E'} },
{ 47, 7, 0, {'S', 'S'} },
{ 48, 6, 0, {'S', 'X'} },
{ 49, 5, 0, {'S', 'F'} },
{ 50, 4, 0, {'S', 'R'} },
{ 51, 3, 0, {'S', 'H'} },
{ 52, 2, 0, {'S', 'W'} } };
printf ("Welcome to B L A C K J A C K . . .\n"); /* opening title */
printf ("Pick a seed number:"); /* opportunity to set the seed in */
fflush(stdin);
scanf ("%f", &seed); /* the random function */
printf ("\nWelcome!!! Shortly you will encounter a superb game but...\n");
printf ("first let me explain that each player is dealt a random card on\n");
printf ("each player's turn and the first number for each player is the card\n");
printf ("number from one to fifty-two, fifty-two being the number of cards in\n");
printf ("a deck. Immediately following this number is the card suit and then the\n");
printf ("card rank. These values are abbreviated but you can guess them right.\n");
printf ("The last number is the card's value which we hope will add in the\n");
printf ("player's behalf to 21! Here we go and have fun!\n");
srand (seed); /* function to set the seed */
taken_loop1: i = rand ()%52 + 1; /* random card generator used later too */
deck[i].taken = 1; /* this card is now taken */
if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
{ /* if */
printf ("card value = %d\n", deck[i].value);/* report the face value oface*/
printf ("Would you like this card (ace) to be valued at 1 or 11? ");
scanf ("%d", &ace); /* asks player # 1 for value of this ace */
deck[i].value = ace; /* new face value of the ace */
}
printf ("Player # 1 has a: %d %c%c %d\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
sum1 = deck[i].value; /* sum1 sets original sum of first card after the */
i = rand ()%52 + 1; /* random card is selected after seed and cutting */
if (deck[i].taken) /* check to see if the card is already used */
{
printf ("card already taken %d ", deck[i].card_number); /* write the used*/
goto taken_loop1; /* card number to screen and proceed to pick another */
}
else
deck[i].taken = 1; /* this card is now taken */
if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
{ /* if */
printf ("card value = %d\n", deck[i].value);/* report the face value oface*/
printf ("Would you like this card (ace) to be valued at 1 or 11? ");
scanf ("%d", &ace); /* asks player # 1 for value of this ace */
deck[i].value = ace; /* new face value of the ace */
}
printf ("Player # 2 has a: %d %c%c %d\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
sum2 = deck[i].value; /* sum2 sets sum for player number two */
while ((sum2 < 22 && sum1 < 22) || (sum1 == 21 || sum2 == 21) || (choice1 && choice2 == 'n'))
{ /* while */
printf ("Player # 1: Do you want another card? y/n\n"); /* computer asks */
fflush(stdin); /* flush buffer in keyboard */
choice1 = getchar (); /* receive from keyboard input */
choice1 = tolower(choice1); /* make lowercase */
if (choice1 == 'y') /* if */
{ /* card is chosen but not taken */
taken_loop2: i = rand ()%52 + 1; /* test in a loop a random number */
if (deck[i].taken) /* if */
{
printf ("card already taken %d", deck[i].card_number);/* write the used */
goto taken_loop2; /* receive a new card from the deck */
} /* if card is new go on */
else /* else */
deck[i].taken = 1; /* this card is now taken */
if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
{ /* if */
printf ("card value = %d\n", deck[i].value);/* report the face value oface*/
printf ("Would you like this card (ace) to be valued at 1 or 11? ");
scanf ("%d", &ace); /* asks player # 1 for value of this ace */
deck[i].value = ace; /* new face value of the ace */
}
printf ("Player # 1 has a: %d %c%c %d also\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
value3 = deck[i].value; /* assign a temp variable the face value of card */
sum1 = sum1 + value3; /* update the player's sum */
printf ("Player # 1: Your sum now is: %d\n", sum1); /* report sum */
if (sum1 == 21) /* if */
{
taken_loopspecial: i = rand ()%52 + 1; /* special loop if # 1 player = 21 */
if (deck[i].taken) /* test for a new or used card */
{
printf ("card already taken %d", deck[i].card_number);
goto taken_loopspecial;/* goto special loop when player #2 gets a new card*/
}
else printf ("Player # 2 has a %d %c%c %d also\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
value4 = deck[i].value; /* assign a temp variable the face value of card */
sum2 = sum2 + value4; /* update the player's sum */
if (sum2 == 21) /* if */
printf ("Game is declared a tie at 21 all!!!!");/* both sums were tested */
else printf ("Player # 1 wins blackjack!"); /* else Player #1 has 21 */
exit (EXIT_SUCCESS); /* exit successfully */
}
if (sum1 > 21) /* if */
{
printf ("Player # 2 wins blackjack!"); /* Player # 1's sum exceeded 21 */
exit (EXIT_SUCCESS); /* exit successfully */
}
}
else /* else */
{
printf ("Player # 1 stands."); /* if player # 1 wants no card this round */
flag = flag + 1;/* flag variable to see if both players want no more cards */
if (flag == 2) /* if so break out of while loop */
break;
}
printf ("Player # 2: Do you want another card? y/n\n");/* Player # 2's turn*/
fflush(stdin); /* flush keyboard */
choice2 = getchar (); /* receive decision from keyboard */
choice2 = tolower(choice2); /* make lowercase */
if (choice2 == 'y') /* if */
{
taken_loop3: i = rand ()%52 + 1; /* test in a loop a random number */
if (deck[i].taken) /* see if card has already been used */
{
printf ("card already taken %d", deck[i].card_number);/* write used number*/
goto taken_loop3; /* receive a new card from the deck */
}
else /* else */
deck[i].taken = 1; /* this card is now taken */
if ((deck[i].card_number == 1) || (deck[i].card_number == 14) || (deck[i].card_number == 27) || (deck[i].card_number == 40))
{ /* if */
printf ("card value = %d\n", deck[i].value);/* report the face value oface*/
printf ("Would you like this card (ace) to be valued at 1 or 11? ");
scanf ("%d", &ace); /* asks player # 1 for value of this ace */
deck[i].value = ace; /* new face value of the ace */
}
printf ("Player # 2 has a: %d %c%c %d also\n", deck[i].card_number, deck[i].suit_rank[0], deck[i].suit_rank[1], deck[i].value);
value4 = deck[i].value;/* assign a temp variable the face value of the card */
sum2 = sum2 + value4; /* update the sum of Player # 2 */
printf ("Player # 2: Your sum now is: %d\n", sum2); /* report sum of # 2 */
if (sum2 > 21) /* if */
{
printf ("Player # 1 wins blackjack!"); /* test if Player # 2 exceeded 21 */
exit (EXIT_SUCCESS); /* exit successfully */
}
else if (sum2 == 21) /* else if */
{
printf ("Player # 2 wins.\n"); /* Player # 2 went second so is the final */
exit (EXIT_SUCCESS); /* qualifier */ /* exit successfully */
}
}
else /* else */
{
printf ("Player # 2 stands."); /* Player # 2 wants no more cards this round*/
flag = flag + 1;/* flag variable to see if both players want no more cards */
if (flag == 2) /* if so break out of while loop */
break;
}
} /* while */
if (sum1 > sum2) /* see if player # 1 has higher sum after both want */
printf ("Player # 1 is victorious!"); /* no more cards once each */
if (sum2 > sum1) /* see if player # 2 has higher sum after both want */
printf ("Player # 2 is victorious!"); /* no more cards once each */
} /* main */